﻿using LogLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace KAImage.Algorithms
{
    public static class ImageAdjustting
    {
        #region Other adjustings
        // Lightness Adjust
        public static void Lightness(Bitmap curBitmap, int width, int height, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            if (delta > 0)
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                    rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                    rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                }
            }
            else
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    rgbValues[i] = (byte)Math.Max(0, rgbValues[i] + delta);
                    rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] + delta);
                    rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] + delta);
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Saturation Adjust
        public static void Saturation(Bitmap curBitmap, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = curBitmap.Width * curBitmap.Height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i = i + 4)
            {
                if (rgbValues[i] > 128)
                {
                    rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                }
                else
                {
                    rgbValues[i] = (byte)Math.Max(0, rgbValues[i] - delta);
                }
                if (rgbValues[i + 1] > 128)
                {
                    rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                }
                else
                {
                    rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] - delta);
                }
                if (rgbValues[i + 2] > 128)
                {
                    rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                }
                else
                {
                    rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] - delta);
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Contrast Adjust
        public static void Contrast(Bitmap curBitmap, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = curBitmap.Width * curBitmap.Height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            if (delta > 0)
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    if (rgbValues[i] > 128 && rgbValues[i + 1] > 128 && rgbValues[i + 2] > 128)
                    {
                        rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                        rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                        rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                    }
                    if (rgbValues[i] < 128 && rgbValues[i + 1] < 128 && rgbValues[i + 2] < 128)
                    {
                        rgbValues[i] = (byte)Math.Max(0, rgbValues[i] - delta);
                        rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] - delta);
                        rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] - delta);
                    }
                }
            }
            else
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    if (rgbValues[i] > 128 && rgbValues[i + 1] > 128 && rgbValues[i + 2] > 128)
                    {
                        rgbValues[i] = (byte)Math.Max(128, rgbValues[i] + delta);
                        rgbValues[i + 1] = (byte)Math.Max(128, rgbValues[i + 1] + delta);
                        rgbValues[i + 2] = (byte)Math.Max(128, rgbValues[i + 2] + delta);
                    }
                    if (rgbValues[i] < 128 && rgbValues[i + 1] < 128 && rgbValues[i + 2] < 128)
                    {
                        rgbValues[i] = (byte)Math.Min(128, rgbValues[i] - delta);
                        rgbValues[i + 1] = (byte)Math.Min(128, rgbValues[i + 1] - delta);
                        rgbValues[i + 2] = (byte)Math.Min(128, rgbValues[i + 2] - delta);
                    }
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Gamma Adjust
        public static void Gamma(Bitmap curBitmap, int width, int height, double delta)
        {
            if (delta == 1)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i = i + 4)
            {
                rgbValues[i] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i] / 255.0, delta)));
                rgbValues[i + 1] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i + 1] / 255.0, delta)));
                rgbValues[i + 2] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i + 2] / 255.0, delta)));
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }
        #endregion
    }
}
